package com.atguigu.dga.governance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.mapper.GovernanceAssessGlobalMapper;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * <p>
 * 治理总考评表 服务实现类
 * </p>
 *
 * @author liuxu
 * @since 2023-10-16
 */
@Service
@DS("dga")
public class GovernanceAssessGlobalServiceImpl extends ServiceImpl<GovernanceAssessGlobalMapper, GovernanceAssessGlobal> implements GovernanceAssessGlobalService {

    @Override
    public void calcGovernanceAssessGlobal(String assessDate) {
        //幂等处理
        remove(
                new QueryWrapper<GovernanceAssessGlobal>().eq("assess_date",assessDate)
        );

        GovernanceAssessGlobal governanceAssessGlobal = getBaseMapper().selectGovernanceAssessGlobal(assessDate);

        save(governanceAssessGlobal);
    }

    @Override
    public String getGlobalScoreGlobalService() {
        //取最后一天的一条数据
        GovernanceAssessGlobal assessDate = getOne(
                new QueryWrapper<GovernanceAssessGlobal>().
                        orderByDesc("assess_date").
                        last("limit 1")
        );
        //保存五维分数
        ArrayList<BigDecimal> scoreList = new ArrayList<>();
        scoreList.add(assessDate.getScoreSpec());
        scoreList.add(assessDate.getScoreStorage());
        scoreList.add(assessDate.getScoreCalc());
        scoreList.add(assessDate.getScoreQuality());
        scoreList.add(assessDate.getScoreSecurity());
        //转成json格式
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate",assessDate.getAssessDate());
        jsonObject.put("sumScore",assessDate.getScore());
        jsonObject.put("scoreList",scoreList);

        return jsonObject.toJSONString();
    }
}
